const app = new Vue({
  el: '#app',
  data: {
    books: [
      {
        id: 1,
        name: '《算法导论》',
        date: "2006-9",
        price: 85.00,
        count: 1
      },
      {
        id: 2,
        name: '《UNIX编程艺术》',
        date: "2006-2",
        price: 59.00,
        count: 1
      },
      {
        id: 3,
        name: '《编程珠玑》',
        date: "2008-10",
        price: 39.00,
        count: 1
      },
      {
        id: 4,
        name: '《代码大全》',
        date: "2006-8",
        price: 128.00,
        count: 1
      },
    ]
  },
  computed: {
    totalPrice() {
      let totalPrice = 0.00;
      /*    for (let i = 0; i <this.books.length ; i++) {
            totalPrice+= this.books[i].price * this.books[i].count;
          }*/
      /* for (let i in this.books) {
         totalPrice+= this.books[i].price * this.books[i].count;
       }*/
      // for (let book of this.books) {
      //   totalPrice +=book.count*book.price
      // }
      // return totalPrice;
      return this.books.reduce((a, n) => {
        return a + n.price * n.count
      }, 0)
    }
  },
  methods: {
    /*  getFinalPrice(price){
        return "￥"+price.toFixed(2)
      }*/
    increment(index) {
      this.books[index].count++

    },
    decrement(index) {
      this.books[index].count--;

    },
    removeHandle(index) {
      this.books.splice(index, 1)
    }
  },
  filters: {
    getPriceFilter(price) {
      return "￥" + price.toFixed(2)
    }
  }


})
//filter中的回调函数有一个要求：必须返回一个boolean值
//true:当返回true时，函数内部会将这次回调的n加入到一个新数组中
//false ：当返回false时，函数会过滤掉这个n

const nums = [10, 20, 111, 222, 444, 40, 50]
//回调写法
/*let newNums =nums.filter(function (n){
  return n < 100
})*/
//转换箭头写法
let newNums = nums.filter((n) => {
  return n < 100
})
//简写
/*let newNums =nums.filter(n=>n<100)*/
console.log(newNums)

//map函数的使用
// let new2Nums=newNums.map(function (n){
//   return n*2
// })

let new2Nums = newNums.map(n => n * 2)
console.log(new2Nums)

//reduce函数的使用,求和 参数a是前面return的
// let totalNums = new2Nums.reduce(function (a,n){
//   return a+n;
//
// })
let totalNums = new2Nums.reduce((a, n) => a + n);
//链式编程
let totalNums1 = nums.filter(n => n < 100).map(n => n * 2).reduce((a, n) => a + n)
console.log(totalNums)
console.log(totalNums1)
